﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using BrainTechLLC.Silverlight.Controls;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using ProModel.ApplicationFramework.Silverlight;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class ManageCharacters : UserControl
    {
        protected ShowCharacterDetails1 _showDetails;
        protected ModalControl _wmc;
        public event EventHandler<ObjectEventArgs<GameChar>> WantEnterGame;

        public static readonly DependencyProperty GameCharsProperty = DependencyProperty.Register(
           "GameChars", typeof(ObservableCollection<GameChar>), typeof(ManageCharacters),
            new PropertyMetadata(null, new PropertyChangedCallback(GameCharsChanged)));

        public ObservableCollection<GameChar> GameChars
        {
            get { return (ObservableCollection<GameChar>)GetValue(GameCharsProperty); }
            set { SetValue(GameCharsProperty, value); }
        }

        private static void GameCharsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ManageCharacters t = (ManageCharacters)d;
            t.GameChars = e.NewValue as ObservableCollection<GameChar>;
            t.SetGameCharData();
        }

        public void SetGameCharData()
        {
            if (lstGameChars != null)
                lstGameChars.ItemsSource = GameChars;
        }

        public void BindToGameCharData(List<GameChar> gameChars)
        {
            ObservableCollection<GameChar> results = new ObservableCollection<GameChar>();

            var v = gameChars;
            foreach (var item in v)
            {
                GameChar gchar = item as GameChar;

                if (gchar != null)
                    results.Add(gchar);
            }

            GameChars = results;
        }


        public ManageCharacters()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetGameCharData();
        }

        private void lstGameChars_Loaded(object sender, RoutedEventArgs e)
        {
            //FrameworkElement t = sender as FrameworkElement;
            //t.StretchListBox();
        }

        void details_WantClose(object sender, EventArgs e)
        {
            _wmc.Modal.HideModal();
            _showDetails.Visibility = Visibility.Collapsed;
        }

        void details_WantEnterGame(object sender, ObjectEventArgs<GameChar> e)
        {
            _wmc.Modal.HideModal();

            if (WantEnterGame != null)
                WantEnterGame(this, e);
        }

        bool OnDialogClosed(object sender, DialogExit e)
        {
            MessageBox.Show("Dialog Result: " + e.ToString());
            return true;
        }

        private void btnViewChar_Click(object sender, RoutedEventArgs e)
        {
            if (_showDetails != null)
            {
                _showDetails.WantEnterGame -= details_WantEnterGame;
                _showDetails.WantClose -= details_WantClose;
            }

            GeneralTransform gt = (sender as Control).TransformToVisual(LayoutRoot5);
            Point p = gt.Transform(new Point(0, 0));

            double detailsWidth = 700;
            double detailsHeight = 460;
            _wmc = new ModalControl(new DialogClose(OnDialogClosed));
            ShowCharacterDetails1 cd = new ShowCharacterDetails1()
            {
                Name = "showDetails",
                VerticalAlignment = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                ShowEnter = true
            };
            cd.Margin = new Thickness(0, 0, 0, 0);

            Canvas.SetZIndex(cd, 888);
            _showDetails = cd;
            _showDetails.WantEnterGame += new EventHandler<ObjectEventArgs<GameChar>>(details_WantEnterGame);
            _showDetails.WantClose += new EventHandler(details_WantClose);
            _showDetails.GameChar = (sender as Button).DataContext as GameChar;
            _showDetails.HorizontalAlignment = HorizontalAlignment.Stretch;
            _showDetails.VerticalAlignment = VerticalAlignment.Top;
            _showDetails.Width = double.NaN;// detailsWidth;
            _showDetails.Height = double.NaN;// detailsHeight;
            _showDetails.MaxHeight = detailsHeight;

            _wmc.HostedControl = cd;
            _wmc.ShowModal(LayoutRoot5, true, new Size(detailsWidth, detailsHeight));
        }
    }

    //public static class UT
    //{
    //    public static object FindInHeirarchy(this object o, Type t)
    //    {
    //        if (o == null)
    //            return null;

    //        if (t.IsAssignableFrom(o.GetType()))
    //            return o;

    //        Control c = o as Control;

    //        if (c != null && c.Parent != null)
    //            return FindInHeirarchy(c.Parent, t);

    //        Panel p = o as Panel;

    //        if (p != null && p.Parent != null)
    //            return FindInHeirarchy(p.Parent, t);

    //        Border b = o as Border;

    //        if (b != null && b.Parent != null)
    //            return FindInHeirarchy(b.Parent, t);


    //        return null;
    //    }

    //    public static T FindInHeirarchy<T>(this Control c) where T : class
    //    {
    //        return FindInHeirarchy(c, typeof(T)) as T;
    //    }
    //}
}